﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Phieuthu.aspx.cs" Inherits="WebTeamplate.Admin_Phieuthu" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

  <%-- <script type="text/javascript" src="../Js/jquery-1.7.1.min.js"></script>

    <script type="text/javascript" src="../Js/jquery.numeric.js"></script>
--%>
    <script src="../Js/BasicFunction.js"></script>

    <script src="../Js/ClientScript.js"></script>

    <script src="../Js/formatNumber.js"></script>

</head>
<body>
    <form id="form1" runat="server">
    <table align="center" width="600" cellspacing="1" cellpadding="0" border="0" class="listForm">
        <tbody>
            <tr>
                <td class="detailedViewHeader" colspan="6">
                    <b>Quản lý phiếu thu tiền</b>
                </td>
            </tr>
            <tr align="right" class="rowDetail">
                <td class="dvtCellInfo" colspan="2">
                    (<span class="commentRequireInput">*</span>): <span class="commentRequireInputText">
                        <%=Resources.Resource.LABEL_COMMENT_REQUIRE %>
                    </span>
                </td>
            </tr>
            <tr class="rowDetail">
                <td align="right" class="dvtCellLabel" width="100px">
                    Mã phiếu bán hàng:
                </td>
                <td align="left" class="dvtCellInfo" width="400px">
                    <asp:TextBox ID="txtMaPhieuBanHang" Enabled="false" Style="width: 90px;" CssClass="detailedViewTextBoxList_Disable1 ctrListForm"
                        runat="server"></asp:TextBox>
                        <input type="hidden" id="hidMaPT" name="hidMaPT" value="" />
                </td>
            </tr>
            <tr class="rowDetail">
                <td align="right" class="dvtCellLabel" width="120px">
                    Tiền thu(<span class="commentRequireInput">*</span>):
                </td>
                <td align="left" class="dvtCellInfo">
                    <asp:TextBox ID="txtValue" Width="190" onblur="this.value=formatNumberAutoSeparate(this.value);" class="detailedViewTextBoxList ctrListForm"
                        runat="server">
                    </asp:TextBox>
                    <asp:RequiredFieldValidator ID="refvValue" ControlToValidate="txtValue" runat="server"
                        SetFocusOnError="true" Display="Dynamic">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr class="rowDetail">
                <td align="right" class="dvtCellLabel" width="120px">
                    Ghi chú:
                </td>
                <td align="left" class="dvtCellInfo">
                    <asp:TextBox ID="txtComment" TextMode="MultiLine" Width="400" Height="120" runat="server">
                    </asp:TextBox>
                    <input type="hidden" name="hidRow" id="hidRow" />
                </td>
            </tr>
            <tr>
                <td align="center" valign="bottom" colspan="2" style="height: 40px;">
                    <input type="button" class="crmbutton 
listForm edit" id="btnSave" onclick="return Save();"
                        value="Lưu" name="btnSave">
                    <input type="button" class="crmbutton 
listForm edit" id="btnClose" onclick="return Close();"
                        value="Đóng" name="btnClose">
                </td>
            </tr>
        </tbody>
    </table>

    <%--<script type="text/javascript">
        $("#txtValue").numeric();
    </script>--%>

    <script type="text/javascript">
        function LoadDataEdit(id) {
            document.getElementById('hidRow').value = id;
            var data = parent.document.getElementById('hidPhieuThu').value;
            var arr = new Array();
            arr = data.split('*');
            if (arr.length > 1) {
                var index = parseInt(id)
                var editData = arr[index].toString();
                arr = editData.split('|');
                if (arr.length > 1) {
                    var id = arr[0].toString()
                    var tien = arr[1].toString();
                    var note = arr[2].toString();

                    document.getElementById('hidMaPT').value = id
                    document.getElementById('txtValue').value = tien;
                    document.getElementById('txtComment').value = note;
                }
            }
        }
        function Close() {
            parent.$.nyroModalRemove();
        }
        var genIndex = 1;
        function Save() {
            debugger;
            var tien = document.getElementById('txtValue').value;
            tien = ReplaceAllChar(removeComma(tien), ",", ".");
            tien = parseFloat(tien);
            if (isNaN(tien))
                tien = 0;
            if (tien <1) {
                alert('Tiền thu không hợp lệ');
                return false;
            }
            else {
                // tien = formatNumberAutoSeparate(ReplaceAllChar(tien.toString(), ".", ","));
                var id = document.getElementById('hidMaPT').value;
                var note = document.getElementById('txtComment').value;
                var currentValue = parent.document.getElementById('hidPhieuThu').value;
                var row = document.getElementById('hidRow').value;
                row = parseInt(row);
                if (isNaN(row))
                    row = 0;
                if (row < 1)
                    parent.document.getElementById('hidPhieuThu').value = currentValue + '*' + id + '|' + tien + '|' + note;
                else {
                    var result = '*';
                    var arr = new Array();
                    arr = currentValue.split('*');
                    if (arr.length > 1) {

                        for (var i = 0; i < arr.length; i++) {
                            if (i == row) {
                                if (result == '*')
                                    result += id + '|' + tien + '|' + note;
                                else
                                    result += '*' + id + '|' + tien + '|' + note;
                            }
                            else {
                                if (arr[i].toString() != '') {
                                    if (result == '*')
                                        result += arr[i].toString();
                                    else
                                        result += '*' + arr[i].toString();
                                }
                            }
                        }

                    }
                    parent.document.getElementById('hidPhieuThu').value = result;
                }
                //generate text display
                debugger;
                //var text = parent.GenerateTextDisplay
                parent.GenerateTextDisplay();
                
                //alert(text);
               // parent.document.getElementById('phieuthuContent').innerHTML = text;
            }
            parent.$.nyroModalRemove();

        }
        function ParseStringToMoney(value) {
            return ReplaceAllChar(removeComma(value), ",", ".");
        }
        var tong = 0;
        function GenerateTextDisplay() {
            var data = parent.document.getElementById('hidPhieuThu').value;
            var arr = new Array();
            arr = data.split('*');
            if (arr.length > 0) {
                var result = '<table>';


                for (var i = 0; i < arr.length; i++) {
                    result += GenerateDisplayRow(i + 1, arr[i].toString());
                }
                var thanhtien = parent.document.getElementById('txtThanhTienVAT').value;
                thanhtien = ReplaceAllChar(removeComma(thanhtien), ",", ".");
                thanhtien = parseFloat(thanhtien);
                if (isNaN(thanhtien))
                    thanhtien = 0;
                
                if (tong > 0) {
                
                    result += " <tr>"
                    result += "    <td style='width:60px'>Tổng:</td>"
                    result += "    <td align='right' style='min-width:60px;padding-left:10px'>" + formatNumberAutoSeparate(ReplaceAllChar(tong.toString(), ".", ",")) + "</td>";
                    result += "    <td align='left'></td>";
                    result += " </tr>"
                }
                if (thanhtien > 0 && tong >= thanhtien) {
                    parent.document.getElementById('chkCommit').disabled = false;
                }
                else {
                    parent.document.getElementById('chkCommit').checked=false;
                    parent.document.getElementById('chkCommit').disabled = true;
                }
                parent.document.getElementById('hidTongTienThu').value = tong;
                
                result += '</table>';
                return result;
            }
        }
        function GenerateDisplayRow(index, data) {
            debugger;
            if (data == '')
                return '';
            var arr = new Array();
            arr = data.split('|');
         
            if (arr.length > 1) {

                var tien = arr[0].toString();
                var oldValue = ReplaceAllChar(removeComma(tien), ",", ".");
                oldValue = parseFloat(oldValue);
                if (isNaN(oldValue))
                    oldValue = 0;
                var note = arr[1].toString();
                var result = '<tr>'
                result += "    <td style='width:60px'>Lần " + genIndex + ":</td>"
                result += "    <td align='right' style='min-width:60px;padding-left:10px'>" + tien + "</td>";
                result += "    <td align='left' style='padding-left:10px;'><a onClick='Edit(" + genIndex + ")' href='#'>>Cập nhật</a></td>";
                result += "    <td align='left' style='padding-left:10px;'><a onClick='Delete(" + genIndex + ")' href='#'>>Xóa</a></td>";
                genIndex++;
                tien = parseFloat(tien)
                tong += oldValue;
                return result;
            }
        }
       
    </script>

    <asp:Literal ID="lblScript" runat="server"></asp:Literal>
    </form>
</body>
</html>
